package com.situ.project1.controller;

import com.github.pagehelper.PageInfo;
import com.situ.project1.pojo.Car;
import com.situ.project1.pojo.Employee;
import com.situ.project1.service.EmployeeService;
import com.situ.project1.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author:yzy
 * @date:2024/8/10
 */
@RestController
@RequestMapping(value = "/api/employee", produces = "application/json;charset=utf-8")
public class EmployeeController {
    private final EmployeeService employeeService;
    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }
    @GetMapping
    public Map<String,Object> findAll(
            @RequestParam (defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "2147483647") Integer pageSize,
            Employee employee
    ){
        if (pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize < 10) {
            pageSize = Integer.MAX_VALUE;
        }
        System.out.println(employee.getEmpId());
        PaginateInfo pi =new PaginateInfo(pageNo,pageSize);
        List<Employee> employees = employeeService.findAll(pi,employee);
        PageInfo<Employee> pif = new PageInfo<>(employees);
        Map<String,Object> map = Map.of("total",pif.getTotal(),"pageNo",pif.getPageNum(),"pageSize",pif.getPageSize());
        return Map.of("success",true,"rows",employees,"pi",map);
    }
    @PostMapping
    public Map<String,Object> add(@RequestBody Employee employee){
        if (
                employee.getEmpId() == null ||
                employee.getEmpName() == null ||
                employee.getPhone() == null ||
                employee.getStatus() == null||
                employee.getSex() == null
        ){
            return Map.of("success",false,"msg","参数错误");

        }else {
            boolean success = employeeService.add(employee);
            return Map.of("success",success);
        }
    }
    @PutMapping
    public Map<String,Object> update(@RequestBody Employee employee){
        boolean success = employeeService.update(employee);
        return Map.of("success",success);
    }
    @DeleteMapping
    public Map<String,Object> delete(@RequestBody Integer ids[]){
        boolean success = employeeService.delete(ids);
        return Map.of("success",success);
    }


    //    给一个时间，查出来这个时间一个小时以内没有订单的小伙子
    @GetMapping("/rest")
    public Map<String,Object> findRest(@RequestParam LocalDateTime startTime){
        LocalDateTime endTime = startTime.plusHours(1);
        System.out.println(startTime);
        System.out.println(endTime);
        List<Employee> emps = employeeService.findRest(startTime,endTime);
        System.out.println(emps);
        return Map.of("success",true,"rows",emps);
    }

//    根据返回的姓名查找id
    @GetMapping("findEmpId")
    public Map<String,Object> findEmpId(@RequestParam String empName){
        Employee employee = employeeService.findEmpId(empName);
        return Map.of("success",true,"rows",employee);
    }

}
